
import socket

# 1.创建一个socket
# 参数1: 指定协议 AF_INET 或 AF_INET6
# 参数2: SOCK_STREAM执行使用面向流的TCP协议
sok = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 2.简历连接

sok.connect(('www.baidu.com', 80))

sok.send(b'GET / HTTP/1.1\r\nHost: www.baidu.com\r\nConnection: close\r\n\r\n')

#等待接收数据
data = []
while True:
    d = sok.recv(1024)
    if d:
        data.append(d)
    else:
        break;
datastr = (b''.join(data)).decode("utf-8")

sok.close()

header, html = datastr.split('\r\n\r\n', 1)

print("header = \r\n" + header)
print("html = \r\n" + html)
